package com.hpeng.daily.action;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardResidualAction")
@Scope("prototype")
public class AssetCardResidualAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	private List<Lookup> ls;// 用于页面上显示资产的状态的list
	
	private String idArray;
	private AssetCard assetCard;
	private String msg;
	private String catid;
	private String result;
	
	//条件查询
	private String conditionValue;
	private String condition;
	private String acstatusValue;
	
	//用于根据权限生成功能按钮
	private String FN;
	private String accessRight;
	
	@Resource
	private Function function;
	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private LoginUserMgr loginUser;
	
	
/******************************资产残值管理********************************************/	
	public String searchResidual(){
		String order = " ";
		String[] hql = { " from AssetCard where acstatus='605' or acstatus like '7%' "};
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("searchResidual.action?FN=" + FN);
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return "success";
	}
	//条件查询
	public String queryResidualAssetCard(){
		try {
			if(conditionValue!=null){
			       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
					"UTF-8");
			       }
			String append = "";
			if ("zclb".equals(condition)) {
				append = " and category.catname like ('%"+conditionValue+"%') ";
			} else if ("zczt".equals(condition)) {
				append = " and acstatus like ('%"+acstatusValue+"%') ";
			} 
			String[] hql = {
					" from AssetCard where acstatus like '605' or acstatus like '7%' ",
					append };
			String order = " order by acid asc";
			setPagination(assetCardManager.getPagination(page, hql, order));
			pageLink = pagination
					.getPageLink("queryResidualAssetCard.action?condition=" + condition
							+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}
	//点击树上的资产类别得到对应的资产情况
	public String queryResidualByCategory(){
		String[] hql = { " from AssetCard where acstatus='605' and catid like '"
				+ catid + "%' or catid like '" + catid + "%' and acstatus like '7%' " };
		String order = " ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("queryResidualByCategory.action?FN=" + FN
				+ "&catid=" + catid);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	//   状态：残值-部门经办已申请
	public String updateResidualJBAsk(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("605")){
			     function.updStatus("7", "50", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("7", "50"));
			  }else{
				msg=getText("物资部门经办只能申请状态处于报废物资部已确认的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("申请失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("申请成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchResidual();
	}
	//  状态：残值-部门经办已上报
	public String updateResidualJBReport(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("750")){
			     function.updStatus("7", "51", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("7", "51"));
			  }else{
				msg=getText("物资部门经办只能上报状态处于残值部门经办已申请的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("上报失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("上报成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchResidual();
	}
	//  状态： 残值-部门领导已审核    分两种情况1、审核通过     2、审核不通过  则状态为：残值--物资部经办已申请750
	public String updateResidualLDExamine(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("751")){
			     function.updStatus("7", "52", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),result,function.getStatusDesc("7", "52"));
			  }else{
				msg=getText("物资部门领导只能审核状态处于残值部门经办已上报的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("审核失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("审核成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchResidual();
	}
	//  状态：残值-部门领导已上报
	public String updateResidualLdReport(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("752")){
			     function.updStatus("7", "53", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("7", "53"));
			  }else{
				msg=getText("物资部门领导只能上报状态处于残值部门领导已审核的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("上报失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("上报成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchResidual();
	}
	//   状态:残值-局领导已审核	   分两种情况:1、审核通过  2、审核不通过 则状态为：残值--物资部领导审核752
   public String updateResidualJLDExamine(){
	   try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("753")){
			     function.updStatus("7", "54", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),result,function.getStatusDesc("7", "54"));
			  }else{
				msg=getText("局领导只能审核状态处于残值-部门领导已上报的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("审核失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("审核成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
	   return searchResidual();
   }
	//部门领导退回资产
   public String updateResidualLDBack(){
	   try{
		    String[] stringArray = idArray.split(",");
			for (String id : stringArray) {
				assetCard = assetCardManager.findAssetCard(id);
				if(assetCard.getAcstatus().equals("751")){
					function.updStatus("7", "50", stringArray);
				    assetCardManager.updateAssetCard(assetCard, "E", loginUser
						.getUserId(request()), result,function.getStatusDesc("7", "50"));
				}else{
					msg = getText("只能退回状态为残值-部门经办已上报的资产");
				}
			}
			this.accessRight = function.getAccessRight(session(), FN);
		} catch (Exception rx) {
			msg = getText("退回失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("退回成功");
		}
		return searchResidual();
   }
   //局领导退回资产
   public String updateResidualJLDBack(){
	   try{
		   String[] stringArray = idArray.split(",");
			for (String id : stringArray) {
				assetCard = assetCardManager.findAssetCard(id);
				if(assetCard.getAcstatus().equals("753")){
					function.updStatus("7", "52", stringArray);
				    assetCardManager.updateAssetCard(assetCard, "E", loginUser
						.getUserId(request()), result,function.getStatusDesc("7", "52"));
				}else{
					msg = getText("只能退回状态为残值-部门领导已上报的资产");
				     }
				}
			this.accessRight = function.getAccessRight(session(), FN);
		} catch (Exception rx) {
			msg = getText("退回失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("退回成功");
		}
		return searchResidual();
   }
	
/***************************set   get方法**************************************/
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getPageLink() {
		return pageLink;
	}

	public void setPageLink(String pageLink) {
		this.pageLink = pageLink;
	}

	public Pagination<AssetCard> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}

	public List<Lookup> getLs() {
		return ls;
	}

	public void setLs(List<Lookup> ls) {
		this.ls = ls;
	}

	public String getFN() {
		return FN;
	}

	public void setFN(String fN) {
		FN = fN;
	}

	public String getAccessRight() {
		return accessRight;
	}

	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}
	public Map<String,Object> session(){
		return ActionContext.getContext().getSession();
	}
	public String getIdArray() {
		return idArray;
	}
	public void setIdArray(String idArray) {
		this.idArray = idArray;
	}
	public AssetCard getAssetCard() {
		return assetCard;
	}
	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public HttpServletRequest request(){
		return ServletActionContext.getRequest();
	}
	public String getConditionValue() {
		return conditionValue;
	}
	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public String getAcstatusValue() {
		return acstatusValue;
	}
	public void setAcstatusValue(String acstatusValue) {
		this.acstatusValue = acstatusValue;
	}
	public String getCatid() {
		return catid;
	}
	public void setCatid(String catid) {
		this.catid = catid;
	}
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
}
